package com.haidechizi.leetcode;

/**
 * @author: wangdejun
 * @create: 2020-04-03 10:55
 **/
public class _21_使奇数位于偶数前面 {


    public int[] exchange(int[] nums) {
        int[] result = new int[nums.length];
        int n = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] % 2 == 1) {
                n++;
            }
        }
        int j = 0,k = n;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]%2 == 1) {
                result[j] = nums[i];
                j++;
            } else {
                result[k] = nums[i];
                k++;
            }
        }
        return result;
    }


    public int[] exchange1(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            boolean flag = false;
            for (int j = 0; j < nums.length - i - 1; j++) {
                if (nums[j] % 2 == 0 && nums[j + 1] % 2 == 1) {
                    int temp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = temp;
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
        return nums;
    }
}
